package com.hongdee.scm.approvalflow.controller;

import com.alibaba.druid.util.StringUtils;
import com.hongdee.base.entity.ResultEntity;
import com.hongdee.scm.approvalflow.entity.TScmApprovalflow;
import com.hongdee.scm.approvalflow.service.ITScmApprovalflowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 名  称:TScmApprovalflowController
 * 类  型:class
 * 描  述:
 * 路  径:com.hongdee.scm.approvalflow.controller
 * 作  者: 术洪超
 * 日  期:2017-06-14 10:40
 */
@RestController
@RequestMapping("/scm/approvalflow")
public class TScmApprovalflowController {

    @Autowired
    private ITScmApprovalflowService itScmApprovalflowService;

    @RequestMapping("/info")
    @ResponseBody
    public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "pId", defaultValue = "") String pId) throws Exception {
        ResultEntity res = new ResultEntity();
        if (StringUtils.isEmpty(pId)) {
            throw new Exception("请选择审批！");
        }
        Map map = itScmApprovalflowService.info(pId,uid);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }


    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody TScmApprovalflow data) throws Exception {
        TScmApprovalflow tm = this.itScmApprovalflowService.save(data, uid);
        ResultEntity res = new ResultEntity();
        res.setMsg("保存成功！");
        res.setSuccess(true);
        res.setData(tm);
        return res;
    }

    /**
     * 根据调拨类型查询对应审批流
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestBody Map data
    ) throws Exception {

        Map map = itScmApprovalflowService.page(start, limit, data, uid);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    /**
     * 启用
     * @param uid
     * @param token
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/start")
    @ResponseBody
    public ResultEntity start(@RequestParam(name = "uid", defaultValue = "") String uid,
                              @RequestParam(name = "token", defaultValue = "") String token,
                              @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        itScmApprovalflowService.update(id, "1");
        res.setSuccess(true);
        res.setMsg("启用成功");
        return res;
    }

    /**
     * 停用
     * @param uid
     * @param token
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/stop")
    @ResponseBody
    public ResultEntity stop(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        itScmApprovalflowService.update(id, "0");
        res.setSuccess(true);
        res.setMsg("停用成功！");
        return res;
    }
    /**
     * 根据调拨类型查询是否存在审批开始;返回true为存在开始，否之不存在
     * @param approvalflowPId
     * @param uid
     * @return
     * @throws Exception
     */
    @RequestMapping("/isExistStart")
    @ResponseBody
    public boolean isExistStart(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "approvalflowPId", defaultValue = "") String approvalflowPId) throws Exception {
        return itScmApprovalflowService.isExistApprovalStart(approvalflowPId,uid);
    }
    //单据退回
    @RequestMapping("/backApproval")
    @ResponseBody
    public ResultEntity backApproval(@RequestParam(name = "uid", defaultValue = "") String uid,
                                @RequestParam(name = "token", defaultValue = "") String token,
                                @RequestParam(name = "id", defaultValue = "") String id) throws Exception {

        ResultEntity res = new ResultEntity();
        itScmApprovalflowService.backApproval(id,uid);
        res.setSuccess(true);
        res.setMsg("退回成功！");
        return res;
    }

}
